﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace 声音生成器
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string content = this.content_text.Text;
            int vid = Convert.ToInt32( this.vid.Text);
            if(!String.IsNullOrEmpty(content))
            {
                //准备生成
                byte[] bytes = await SuperTTS.TTS.GetPlayBytes(new string[] {
                    content
                }, vid);
                //获取本地目录
                var saveFile = new FileSavePicker();
                //初始位置
                saveFile.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                // 显示在下拉列表的文件类型
                saveFile.FileTypeChoices.Add("声音", new List<string>() { ".mp3" });
                // 默认的文件名
                saveFile.SuggestedFileName = content + ".mp3";

                StorageFile sFile = await saveFile.PickSaveFileAsync();
                
                if(sFile!=null)
                {
                    using (Stream s = await sFile.OpenStreamForWriteAsync())
                    {
                        await s.WriteAsync(bytes, 0, bytes.Length);
                    }
                }
            }
        }
    }
}
